package gorails.com;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class PickOrigin extends ListActivity {
	private ListView lv;
	private DBAdapter dbAdapter;
	private static final String TABLE_STATION = "station";
	private static final String TABLE_LINE = "line";
	private static final String COLUMN_UPN = "UnionPacificNorth";
	private static final String COLUMN_MDN = "MilwaukeeDistrictNorth";
	private static final String COLUMN_NCS = "NorthCentralService";
	private static final String COLUMN_UPNW = "UnionPacificNorthwest";
	private static final String COLUMN_MDW = "MilwaukeeDistrictWest";
	private static final String COLUMN_UPW = "UnionPacificWest";
	private static final String COLUMN_BNSF = "BNSFRailway";
	private static final String COLUMN_HC = "HeritageCorridor";
	private static final String COLUMN_SWS = "SouthwestService";
	private static final String COLUMN_RID = "RockIslandDistrict";
	private static final String COLUMN_MED = "MetraElectricDistrict";
	private ArrayList<String> list;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lv = getListView();
        lv.setTextFilterEnabled(true);

        //String line = Trip.getInstance().getLine();
        int pos = Trip.getInstance().getLinePosition();
        Log.d("Trip POS", "position= "+pos);
        list = getLineStations(pos);
        
        


        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));

			
    }
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		Log.d("Pick Origin", "station= "+keyword+" pos= "+position);
		Trip.getInstance().setOrigin(keyword);
		
		Trip.getInstance().setStationPosition(position);
		
		
		
		finish();
	}
	public ArrayList<String> getLineStations(int pos){    	    	

		dbAdapter = new DBAdapter(this);
				SQLiteDatabase db =dbAdapter.getDB();
				ArrayList<String> LineList = new ArrayList<String>();
				ArrayList<String> columnList;
				String[] columns = new String[]{COLUMN_UPN,COLUMN_MDN,COLUMN_NCS,COLUMN_UPNW,COLUMN_MDW,COLUMN_UPW,COLUMN_BNSF,COLUMN_HC,COLUMN_SWS,COLUMN_RID,COLUMN_MED};
				Cursor c = db.query(false, TABLE_STATION, columns, null , null, null, null, null, null);
				if (c.moveToFirst()) {
					int j = 0;
			         do{
			        	 columnList = new ArrayList<String>();
			        	 for(int i=0; i<c.getColumnCount(); i++){	 
			        		 if(c.getString(i)!=null&&i==pos){
			        			 //Log.d("PickOrigin", "station= "+c.getString(i));
			        			 columnList.add( c.getString(i) );
			        		 }
			        		 
			        		 
			        	 }	 
			        	 LineList.addAll(columnList);
			        	 j++;
			         } while (c.moveToNext());
			      }
			      if (c != null && !c.isClosed()) {
			         c.close();
			      }
			      db.close();
				return LineList;
	}
}
